#include "../../include/LaserRadar.h"

/**
 * 无参构造
*/
LaserRadar::LaserRadar(/* args */):Component(LASERRADAR) {}
/**
 * 复制构造
*/
LaserRadar::LaserRadar(Component* const component):Component(LASERRADAR,component) {}

LaserRadar::~LaserRadar() {

}


bool LaserRadar::addSubscriber(const string &name) {
    // TODO 异常处理
    subscriberSet.insert(name);
    return true;
}

string LaserRadar::getAllSubscriber() {
    string str = to_string(subscriberSet.size());
    set<string>::iterator iter;
    for(iter=subscriberSet.begin();iter != subscriberSet.end();iter++) {
        str += " " + *iter;
    }
    return str;
}

#include <iostream>
string LaserRadar::updataInfo() {
    cout << "请手动输入激光数据" << endl;
    string laserData;
    cin >> laserData;
    return laserData;
}

string LaserRadar::getInfo() {
    string laserData = updataInfo();
    string allSubscriber = getAllSubscriber();
    return laserData + ":" + allSubscriber;
}

void LaserRadar::doAsCommend(string cmd) {
    int idx = cmd.find(':');
    string carName = cmd.substr(0,idx);
    string subCmd = cmd.substr(idx+1);
    idx = subCmd.find(':');
    string controlCmd = subCmd.substr(0,idx);
    string controlData = subCmd.substr(idx+1);
    if(controlCmd == CAR_CMD_EMPTY) {
        return;
    }
    else if(controlCmd == LASER_ADD_SUBSCRIBER) {
        addSubscriber(controlData);
        cout << controlData << " 订阅了 " << LASERRADAR << endl;
    }
}